//
//  EPMOauthViewController.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/3/23.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMOauthViewController.h"
@interface EPMOauthViewController ()<UIWebViewDelegate>
    @property (strong,nonatomic) UIWebView* webView;
@end

@implementation EPMOauthViewController

- (void)loadView
    {
        self.webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        self.view = self.webView;
    }
    

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
   
    self.webView.delegate = self;
}

#pragma
#pragma-设置界面
    
    - (void)setupUI{
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem EPM_Title:@"取消" imageName:nil target:self action:@selector(cancelbtnClick)];
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem EPM_Title:@"走后门" imageName:nil target:self action:@selector(enjectClick)];
        [self loadOauthPage];
    }
    
#pragma
#pragma-点击方法实现
    - (void)cancelbtnClick{
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
    
    - (void)enjectClick{
//        jsString = "document.getElementById('userId').value = 'leiggee@126.com' , document.getElementById('passwd').value = 'qazwsxedc'"
        NSString* jsString = [NSString stringWithFormat:@"document.getElementById('userId').value = 'leiggee@126.com' , document.getElementById('passwd').value = 'qazwsxedc'"];
//        _webView.stringByEvaluatingJavaScript(from: jsString)
      
        [self.webView stringByEvaluatingJavaScriptFromString:jsString];
    }
#pragma
#pragma-OAth页面展示
    - (void)loadOauthPage{
        NSString* urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",client_id,redirect_uri];
        NSURL * url = [NSURL URLWithString:urlString];
        NSURLRequest* request = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:request];
    }
#pragma
#pragma-监视网络请求

    
    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    //        [SVProgressHUD show];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [SVProgressHUD dismiss];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [SVProgressHUD showWithStatus:@"网络加载错误"];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString* urlString = request.URL.absoluteString;
    NSString* flag = @"code=";
    if ([urlString containsString:flag]){
        
        NSString* quaary = request.URL.query ;
        NSString* code = [quaary substringFromIndex:(flag.length )];
        [[EPMUserAccountModelView sheardTool] geTokenWithCode:code finished:^(bool isSucces) {
            if (!isSucces){
                [SVProgressHUD showWithStatus:@"世界上最遥远的地方是没有网"];
                return ;
            }
            
            [self dismissViewControllerAnimated:YES completion:^{
                [[NSNotificationCenter defaultCenter] postNotificationName:SWITCHROOTVIEWCONTROLLERNOTI object:nil];
            }];
            
        }];
        
        
        return NO;
    }
    
    return YES;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end



